﻿Ext.define('app.system.organization.Index',
{
    extend: 'Ext.panel.Panel',
    border: true,
    layout: 'border',
    initComponent: function () {
        var me = this;
        me.items = [

                    me.getOrganization(),
                    me.getUserList()
        ];
        me.callParent(arguments);
    },
    getOrganization: function () {
        var me = this;
        if (!this.organization) {
            var store = new Ext.data.TreeStore({
                autoLoad: true,
                nodeParam: 'parentId',
                defaultRootId: "0",
                proxy: {
                    // 请求方式
                    type: 'ajax',
                    // 请求网址
                    url: '/api/services/app/organizationUnit/GetOrganizationTree',
                    reader: {
                        type: 'json',
                        rootProperty: function (node) {
                            return node.result || node.children;
                        }
                    }
                }
            });

            this.organization = new Ext.tree.Panel({
                flex: 1,
                region: 'west',
                width: 200,
                tbar: [
                    {
                        text: '新增机构', icon: '/Content/images/add.png', handler: function () {
                            me.newOrganization(null);
                        }
                    }
                ],
                height: '100%',
                border: true,
                title: '组织机构',
                useArrows: true,
                rootVisible: false,
                store: store,
                root: { id: '0', text: '' },
                listeners: {
                    itemclick: function (tree, record, item, index, e, eOpts) {
                        me.getUserList().store.filter = { organizationId: record.data.id };
                        me.getUserList().store.load();
                    },
                    itemcontextmenu: function (menutree, record, item, index, e, eOpts) {
                        e.preventDefault();
                        e.stopEvent();
                        var nodemenu = new Ext.menu.Menu({
                            floating: true,
                            items: [
                            {
                                icon: '/Content/images/add.png',
                                text: "新增机构",
                                handler: function () {
                                    me.newOrganization(record.data.id);
                                }
                            },
                            {
                                icon: '/Content/images/edit.gif',
                                text: "编辑机构",
                                handler: function () {
                                    var win = Ext.create("app.system.organization.Edit", { title: '编辑机构', nodeId: record.data.id, nodeText: record.data.text });
                                    win.on("savesuccess", function () {
                                        me.organization.store.load({
                                            callback: function () {
                                                me.organization.expandAll();
                                            }
                                        });
                                        win.close();
                                    });
                                    win.show();
                                }
                            },
                            {
                                icon: '/Content/images/delete.gif',
                                text: "删除机构",
                                handler: function () {
                                    Ext.Msg.confirm('系统提示', '确定要删除组织机构吗？', function (btn) {
                                        if (btn == 'yes') {
                                            App.AjaxRemove('/api/services/app/organizationUnit/DeleteOrganizationUnit', { id: record.data.id }, function (data) {
                                                me.organization.store.load({
                                                    callback: function () {
                                                        me.organization.expandAll();
                                                    }
                                                });
                                            });
                                        }
                                    });
                                }
                            }]
                        });
                        nodemenu.showAt(e.getXY());
                    },
                    afterrender: function (tree) { tree.expandAll(); }
                }
            });
        }
        return this.organization;
    },
    newOrganization: function (parentNodeId) {
        var me = this;
        var win = Ext.create("app.system.organization.Create", { title: '新增机构', parentNodeId: parentNodeId });
        win.on("savesuccess", function () {
            me.organization.store.load({
                callback: function () {
                    me.organization.expandAll();
                }
            });
            win.close();
        });
        win.show();
    },
    getUserList: function () {
        var me = this;
        if (!this.userList) {
            this.userList = new Ext.grid.Panel({
                title: '组织用户',
                region: 'center',
                tbar: [
                    "->",
                    {
                        text: '新增成员', icon: '/Content/images/add.png', handler: function () {
                            var record = me.organization.getSelection()[0];
                            if (!record) {
                                Ext.Msg.alert("系统提示", "请选择一个组织结构");
                                return false;
                            }
                            var win = Ext.create("app.system.organization.EditMember", { title: '选择组织成员', organizationId: record.data.id });
                            win.on("savesuccess", function () {
                                me.userList.store.load();
                            });
                            win.show();
                        }
                    }
                ],
                viewConfig: {
                    enableTextSelection: true //可以复制单元格文字
                },
                flex: 2,
                multiColumnSort: true,
                forceFit: true,
                store: Ext.create("app.ux.PrmStore", { url: '/api/services/app/OrganizationUnit/GetOrganizationUsers', autoLoad: false }),
                columns: [
                    { header: '账号', dataIndex: 'userName' },
                    { header: '姓名', dataIndex: 'name' },
                    {
                        xtype: 'actioncolumn',
                        width: 35,
                        header: localize('Operating'),
                        items: [
                        {
                            icon: '/Content/images/delete.gif',
                            tooltip: '移除',
                            handler: function (grid, rowIndex, colIndex) {
                                var record = grid.store.getAt(rowIndex);
                                var organization = me.organization.getSelection()[0];
                                Ext.Msg.confirm('系统提示', '确定要移除组织用户吗？',
                                  function (btn) {
                                      if (btn == 'yes') {
                                          App.AjaxRemove('/api/services/app/organizationUnit/RemoveUserFromOrganizationUnit', { UserId: record.data.id, OrganizationUnitId: organization.data.id }, function (data) {
                                              grid.store.removeAt(rowIndex);
                                          });
                                      }
                                  });
                            }
                        }
                        ]
                    }
                ]
            });
        }
        return this.userList;
    }
});
